define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function() {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'meter/index/index',
                    add_url: 'meter/index/add',
                    edit_url: 'meter/index/edit',
                    del_url: 'meter/index/del',
                    multi_url: 'meter/index/multi',
                    table: 'meter',
                }
            })

            var table = $("#table")

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'project',
                            title: __('Project_id'),
                            formatter: function(project) {
                                return project.name
                            }
                        },
                        { field: 'name', title: __('Name') },
                        {
                            field: 'meter_type',
                            title: __('Meter_type'),
                            formatter: function(value) {
                                var statuses = [__('GeneralMeter'), __('PublicMeter'), __('PrivateMeter')]
                                return statuses[value]
                            }
                        },
                        {
                            field: 'userno',
                            title: __('UserNo')

                        },
                        {
                            field: 'content',
                            title: __('Content')

                        },
                        { field: 'create_time', title: __('Create_time'), formatter: Table.api.formatter.datetime },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [{

                                name: 'sycndata',

                                title: '同步数据',

                                icon: 'fa fa-database',

                                classname: 'btn btn-success btn-xs btn-sycndata'

                            }, ]
                        }
                    ]
                ]
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('click', '.btn-sycndata', function(e) {

                e.stopPropagation();

                var options = table.bootstrapTable('getOptions');

                var index = $(this).closest('tr').data('index');

                var data = table.bootstrapTable('getData');

                var id = data[index][options.pk];
                $.post('meter/index/sycndata', { id: id }, function(data) {
                    console.log(data);
                    if (data['code'] == 1) {
                        layer.msg(data['msg']);
                    }
                }, 'json')


            })


        },
        add: function() {
            Controller.api.initCommunity()
            Controller.api.bindevent()
            $("#community_code").change();
        },
        edit: function() {
            Controller.api.initCommunity()
            Controller.api.bindevent()
            $("#community_code").change();
        },
        api: {
            bindevent: function() {
                var $this = this
                Form.api.bindevent($("form[role=form]"))
                $('select[name="row[meter_type]"]').change(function() {
                    if ($(this).val() === '2') {
                        $('.community-box').show()
                    } else {
                        $('.community-box').hide()
                    }
                })
            },
            initCommunity: function() {
                var house_code = $("#house_code").val()
                $("#community_code").bind("change", function() {
                    var community_code = $(this).val()
                    $.ajax({
                        type: "POST",
                        url: 'owners/index/get_house_by_cm_code',
                        async: true,
                        cache: false,
                        dataType: "json",
                        data: { community_code: community_code },
                        success: function(data) {
                            var house = data.house

                            var houseHtml = ''
                            $.each(house, function(index, item) {
                                houseHtml += '<option value="' + item.code + '">' + item.name + '</option>'
                            })
                            if (houseHtml == '') {
                                houseHtml = '<option value="">没有任何选中项</option>'
                            }
                            $("#house_code").html(houseHtml)
                            if (house_code) {
                                $("#house_code").val(house_code)
                            }
                            $("#house_code").selectpicker({
                                showTick: true,
                                liveSearch: true
                            })
                            $("#house_code").selectpicker("refresh")
                        }
                    })
                })
            }
        },
    }
    return Controller
})